package com.first.server.service.impl;


import com.first.common.result.PageResult;
import com.first.common.utils.StringToListUtil;
import com.first.pojo.dto.WorkPageQueryDTO;
import com.first.pojo.entity.UserWork;
import com.first.pojo.entity.Work;
import com.first.pojo.vo.WorkVO;
import com.first.server.mapper.UserWorkMapper;
import com.first.server.mapper.WorkMapper;
import com.first.server.service.WorkService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Slf4j
public class WorkServiceImpl implements WorkService {

    @Autowired
    private WorkMapper workMapper;
    @Autowired
    private UserWorkMapper userworkMapper;
    @Override
    public PageResult pageQuery(WorkPageQueryDTO workPageQueryDTO) {
        PageHelper.startPage(workPageQueryDTO.getPage(), workPageQueryDTO.getPageSize());
        Page<Work> page = workMapper.pageQuery(workPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 添加成果信息
     * @param workVO
     */
    @Transactional
    public void addWork(WorkVO workVO) {
        Work work=Work.builder()
                .workName(workVO.getWorkName())
                .details(workVO.getDetails())
                .build();
        workMapper.insert(work);
        String id = String.valueOf(work.getId());
        List<Integer> AuthorIds=StringToListUtil.StringToList(workVO.getAuthorId());
        for (Integer authorId : AuthorIds) {
            UserWork userWork=UserWork.builder()
                    .authorId(String.valueOf(authorId))
                    .workName(workVO.getWorkName())
                    .workId(id)
                    .build();
            userworkMapper.insert(userWork);
        }
    }
}
